package com.zhangxun.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhangxun.domain.UserAddr;
import com.zhangxun.service.UserAddrService;
import com.zhangxun.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户收货地址管管理接口")
@RestController
@RequestMapping("/p/address")
public class AddressController {

    @Autowired
    private UserAddrService userAddrService;





    @GetMapping("/list")
    @ApiOperation("查询用户所有的收获地址接口")
    public ResponseEntity<List<UserAddr>> list() {
        List<UserAddr> list = userAddrService.list(new LambdaQueryWrapper<UserAddr>()
                .eq(UserAddr::getUserId, SecurityUtil.getWxUserId())
                .eq(UserAddr::getStatus, 1));
        return ResponseEntity.ok(list);
    }


    @PostMapping
    @ApiOperation("保存用户新增的收货地址接口")
    public ResponseEntity<Boolean> saveAddress(@RequestBody UserAddr userAddr){
      boolean result = userAddrService.saveAddr(userAddr);
      return ResponseEntity.ok(result);
    }


    @PutMapping("/defaultAddr/{addrId}")
    @ApiOperation("修改用户默认收货地址接口")
    public ResponseEntity<Boolean> updateDefaultAddress(@PathVariable("addrId") Long addrId){

          Boolean result = userAddrService.updateDefaultAddr(addrId);
          return ResponseEntity.ok(result);

    }


    /**
     * 远程调用通过用户id获取用户默认收货地址
     * @param wxUserId
     * @return
     */
    @GetMapping("/defaultById/{wxUserId}")
    public UserAddr selectDefaultAddr(@PathVariable("wxUserId") String wxUserId){
        UserAddr userAddr = userAddrService.getOne(new LambdaQueryWrapper<UserAddr>()
                .eq(UserAddr::getUserId, wxUserId)
                .eq(UserAddr::getCommonAddr, 1)
                .eq(UserAddr::getStatus, 1));

        return userAddr;
    }
}
